package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm01Currency;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp04Supply;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.fullEconomics.struct.SupplyStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class SupplyForm extends ValidatorForm {
    /**
     * 
     */
    String selectedRowNum = null;
    String modePanel = "";
    String operation = "";
    String supplierF = null;
    String partF = null;
    String gsdbF = null;
    String partCF = null;
    String partPreCF = null;
    String partBaseF = null;
    String partSufixF = null;
    String tvmFindMode = null;
    String dateInEffStart = null;
    String dateInEffEnd = null;
    String dateOutEffStart = null;
    String dateOutEffEnd = null;
    // Objeto que se usara para la creacion
    Teesp04Supply supplyNew = new Teesp04Supply();
    Teesp01Part teesp01Part = new Teesp01Part();
    Teess01Supplier teess01Supplier = new Teess01Supplier();
    Collection<SupplyStruct> supplyS = null;
    Collection<Teesm01Currency> economicMeasure = null;
    String date = "";
    String valueTVMNew = null;
    String procShereNew = null;
    String currencyNew = null;
    String supplierCodeF = null;
    Teesp04Supply supplySelected = new Teesp04Supply();

    public Teesp04Supply getSupplySelected() {
        return supplySelected;
    }

    public void setSupplySelected(Teesp04Supply supplySelected) {
        this.supplySelected = supplySelected;
    }

    public String getSupplierCodeF() {
        return supplierCodeF;
    }

    public void setSupplierCodeF(String supplierCodeF) {
        this.supplierCodeF = supplierCodeF;
    }

    public String getProcShereNew() {
        return procShereNew;
    }

    public void setProcShereNew(String procShereNew) {
        this.procShereNew = procShereNew;
    }

    public String getValueTVMNew() {
        return valueTVMNew;
    }

    public void setValueTVMNew(String valueTVMNew) {
        this.valueTVMNew = valueTVMNew;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void resetForm() {

        this.supplierF = null;
        this.partF = null;
        this.gsdbF = null;
        this.partCF = null;
        this.partPreCF = null;
        this.partBaseF = null;
        this.partSufixF = null;
        this.dateInEffStart = null;
        this.dateInEffEnd = null;
        this.dateOutEffStart = null;
        this.dateOutEffEnd = null;
        this.supplyNew = new Teesp04Supply();
        this.supplyS = null;
        this.tvmFindMode = "-1";
        this.modePanel = "";
        this.operation = "";
        this.procShereNew = null;
    }

    public void resetPanelFind() {
        this.supplierF = "-1";
        this.partF = "";
        this.gsdbF = "";
        this.partCF = "-1";
        this.partPreCF = "";
        this.partBaseF = "";
        this.partSufixF = "";
        this.dateInEffStart = "";
        this.dateInEffEnd = "";
        this.dateOutEffStart = "";
        this.dateOutEffEnd = "";
        this.supplierCodeF = "";
    }

    public void resetPanelCreateUpdate() {
        this.teesp01Part = new Teesp01Part();
        this.teess01Supplier = new Teess01Supplier();
        this.procShereNew = null;
        this.valueTVMNew = null;
        this.currencyNew = null;
    }

    public String getDateInEffEnd() {
        return dateInEffEnd;
    }

    public void setDateInEffEnd(String dateInEffEnd) {
        this.dateInEffEnd = dateInEffEnd;
    }

    public String getDateInEffStart() {
        return dateInEffStart;
    }

    public void setDateInEffStart(String dateInEffStart) {
        this.dateInEffStart = dateInEffStart;
    }

    public String getDateOutEffEnd() {
        return dateOutEffEnd;
    }

    public void setDateOutEffEnd(String dateOutEffEnd) {
        this.dateOutEffEnd = dateOutEffEnd;
    }

    public String getDateOutEffStart() {
        return dateOutEffStart;
    }

    public void setDateOutEffStart(String dateOutEffStart) {
        this.dateOutEffStart = dateOutEffStart;
    }

    public String getGsdbF() {
        return gsdbF;
    }

    public void setGsdbF(String gsdbF) {
        this.gsdbF = gsdbF;
    }

    public String getPartBaseF() {
        return partBaseF;
    }

    public void setPartBaseF(String partBaseF) {
        this.partBaseF = partBaseF;
    }

    public String getPartCF() {
        return partCF;
    }

    public void setPartCF(String partCF) {
        this.partCF = partCF;
    }

    public String getPartF() {
        return partF;
    }

    public void setPartF(String partF) {
        this.partF = partF;
    }

    public String getPartPreCF() {
        return partPreCF;
    }

    public void setPartPreCF(String partPreCF) {
        this.partPreCF = partPreCF;
    }

    public String getPartSufixF() {
        return partSufixF;
    }

    public void setPartSufixF(String partSufixF) {
        this.partSufixF = partSufixF;
    }

    public String getSupplierF() {
        return supplierF;
    }

    public void setSupplierF(String supplierF) {
        this.supplierF = supplierF;
    }

    public Teesp04Supply getSupplyNew() {
        return supplyNew;
    }

    public void setSupplyNew(Teesp04Supply supplyNew) {
        this.supplyNew = supplyNew;
    }

    public Collection<SupplyStruct> getSupplyS() {
        return supplyS;
    }

    public void setSupplyS(Collection<SupplyStruct> supplyS) {
        this.supplyS = supplyS;
    }

    public Teesp01Part getTeesp01Part() {
        return teesp01Part;
    }

    public void setTeesp01Part(Teesp01Part teesp01Part) {
        this.teesp01Part = teesp01Part;
    }

    public Teess01Supplier getTeess01Supplier() {
        return teess01Supplier;
    }

    public void setTeess01Supplier(Teess01Supplier teess01Supplier) {
        this.teess01Supplier = teess01Supplier;
    }

    public String getCurrencyNew() {
        return currencyNew;
    }

    public void setCurrencyNew(String currencyNew) {
        this.currencyNew = currencyNew;
    }

    public Collection<Teesm01Currency> getEconomicMeasure() {
        return economicMeasure;
    }

    public void setEconomicMeasure(Collection<Teesm01Currency> economicMeasure) {
        this.economicMeasure = economicMeasure;
    }

    @SuppressWarnings("unchecked")
    public void supplyMapped(String key, String value) {
        List supplySC = (List) this.getSupplyS();
        SupplyStruct ecoS = (SupplyStruct) supplySC
                .get(Integer.parseInt(key) - 1);
        this.setSupplySelected(ecoS.getTeesp04Supply());
    }

    public String getModePanel() {
        return modePanel;
    }

    public void setModePanel(String modePanel) {
        this.modePanel = modePanel;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public void setEditParametersPanel() {
        setTeesp01Part(getSupplySelected().getTeesp01Part());
        setTeess01Supplier(getSupplySelected().getTeess01Supplier());
        setProcShereNew(getSupplySelected().getEesp04SupplySharePercentP()
                .toString());
        
        if (null!=getSupplySelected().getEesp04SupplyTvmCurrencyC())
        			setCurrencyNew(getSupplySelected().getEesp04SupplyTvmCurrencyC().getEesm01CurrencyCodeC());
        
        
        if (null!=getSupplySelected().getEesp04SupplyTvmAmountA())
        			setValueTVMNew(getSupplySelected().getEesp04SupplyTvmAmountA().toString());
    }

    public void setClearParametersPanel() {
        setTeesp01Part(new Teesp01Part());
        setTeess01Supplier(new Teess01Supplier());
        setProcShereNew("");
        setCurrencyNew("");
        setValueTVMNew("");
    }

    public String getSelectedRowNum() {
        return selectedRowNum;
    }

    public void setSelectedRowNum(String selectedRowNum) {
        this.selectedRowNum = selectedRowNum;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if ((getModePanel().equals("edit") || getModePanel().equals("new"))
                && getOperation().equals("createUpdate")) {
            try {
                if (Double.valueOf(getProcShereNew()) > 100
                        || Double.valueOf(getProcShereNew()) < 0)
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "economicValue.error.porcMenor"));
            } catch (Exception e) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "pricing.error.porc"));
            }
            if (!StringUtils.isBlank(this.getValueTVMNew())) {
                if (getCurrencyNew().equals("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "pricing.error.moneda"));
                }
                try {
                    if (Double.valueOf(getValueTVMNew()) < 0)
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("supply.error.valorTVM"));
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "supply.error.valorTVM"));
                }
            }
        }
        if ("find".equalsIgnoreCase(this.getOperation())) {
            if (!StringUtils.isBlank(this.getDateInEffStart())) {
                if (StringUtils.isBlank(this.getDateInEffEnd())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaIn")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateInEffStart()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateInEffStart()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateInEffStart()));
                }
            }
            if (!StringUtils.isBlank(this.getDateInEffEnd())) {
                if (StringUtils.isBlank(this.getDateInEffStart())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaIn")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateInEffEnd()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateInEffEnd()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateInEffEnd()));
                }
            }
            if (!StringUtils.isBlank(this.getDateOutEffStart())) {
                if (StringUtils.isBlank(this.getDateOutEffEnd())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaOut")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateOutEffStart()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateOutEffStart()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateOutEffStart()));
                }
            }
            if (!StringUtils.isBlank(this.getDateOutEffEnd())) {
                if (StringUtils.isBlank(this.getDateOutEffStart())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "part.error.invalidRange",
                                            resources
                                                    .getMessage("part.label.tableHeader.fechaOut")));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateOutEffEnd()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateOutEffEnd()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateOutEffEnd()));
                }
            }
        }
        return ae;
    }

    public String getTvmFindMode() {
        return tvmFindMode;
    }

    public void setTvmFindMode(String tvmFindMode) {
        this.tvmFindMode = tvmFindMode;
    }
}
